﻿using System.Drawing;
using System.Windows.Forms;

namespace CXW.Core.Sharp
{
    public class WorkbenchLogin
    {
        /// <summary>资源名称:标题</summary>
        public string LanguageTitle { get; set; }
        /// <summary>资源名称:用户名</summary>
        public string LanguageUser { get; set; }
        /// <summary>资源名称:密码</summary>
        public string LanguagePwd { get; set; }
        /// <summary>资源名称:确定按钮</summary>
        public string LanguageOk { get; set; }
        /// <summary>资源名称:取消按钮</summary>
        public string LanguageCancel { get; set; }


        /// <summary>标题</summary>
        public string Title { get; set; }
        /// <summary>用户名</summary>
        public string User { get; set; }
        /// <summary>密码</summary>
        public string PWD { get; set; }
        /// <summary>确定按钮</summary>
        public string Ok { get; set; }
        /// <summary>取消按钮</summary>
        public string Cancel { get; set; }

        /// <summary>获取或设置窗体背景图</summary>
        public Image BackImageFrom { get; set; }
        /// <summary>获取或设置确认按钮背景图</summary>
        public Image BackImageOk { get; set; }
        /// <summary>获取或设置取消按钮背景图</summary>
        public Image BackImageCancel { get; set; }


        /// <summary>获取或设置提示信息</summary>
        public string Message
        {
            set { frm.labMESSAGE.Text = value; }
            get { return frm.labMESSAGE.Text; }
        }
        /// <summary>是否显示边框</summary>
        public bool FormBorder { get; set; }

        Login frm = null;
        public WorkbenchLogin()
        {
            frm = new Login() { };
            string _skinName = PropertyService.Get("Workbench", new Properties()).Get("SkinName", "DevExpressStyle");
            frm.SetLookAndFeel(_skinName);
        }

        /// <summary>启动登陆窗口</summary>
        /// <returns></returns>
        public DialogResult Run()
        {
            frm.FormBorderStyle = (FormBorder ? FormBorderStyle.FixedToolWindow : FormBorderStyle.None);
            frm.Text = string.IsNullOrWhiteSpace(Title) ? ResourceService.GetString(string.IsNullOrWhiteSpace(LanguageTitle) ? frm.AccessibleDescription : LanguageTitle) : Title;
            frm.labUSERID.Text = string.IsNullOrWhiteSpace(User) ? ResourceService.GetString(string.IsNullOrWhiteSpace(LanguageUser) ? frm.labUSERID.AccessibleDescription : LanguageUser) : User;
            frm.labPWD.Text = string.IsNullOrWhiteSpace(PWD) ? ResourceService.GetString(string.IsNullOrWhiteSpace(LanguagePwd) ? frm.labPWD.AccessibleDescription : LanguagePwd) : PWD;
            frm.btnOK.Text = string.IsNullOrWhiteSpace(Ok) ? ResourceService.GetString(string.IsNullOrWhiteSpace(LanguageOk) ? frm.btnOK.AccessibleDescription : LanguageOk) : Ok;
            frm.btnCANCEL.Text = string.IsNullOrWhiteSpace(Cancel) ? ResourceService.GetString(string.IsNullOrWhiteSpace(LanguageCancel) ? frm.btnCANCEL.AccessibleDescription : LanguageCancel) : Cancel;

            frm.BackgroundImage = BackImageFrom;
            frm.btnOK.BackgroundImage = BackImageOk;
            frm.btnCANCEL.BackgroundImage = BackImageCancel;

            frm.btnOK.Click += (sender, e) =>
            {
                ClickOk?.Invoke(frm, frm.txtUSERID.Text.Trim(), frm.txtPWD.Text.Trim());
            };
            frm.btnCANCEL.Click += (sender, e) =>
            {
                ClickCancel?.Invoke(frm);
            };

            return frm.ShowDialog();
        }

        /// <summary>确定按钮事件</summary>
        public event LoginEventHandler ClickOk;
        /// <summary>取消按钮事件</summary>
        public event LoginCancelEventHandler ClickCancel;

        /// <summary>Debug模式时使用</summary>
        /// <param name="user">登陆用户名</param>
        /// <param name="pwd">登陆密码</param>
        public void SetUserPwd(string user, string pwd)
        {
            //设置用户名编辑文本框的内容
            frm.txtUSERID.Text = user;

            //设置密码编辑文本框的内容
            frm.txtPWD.Text = pwd;
        }
    }
    /// <summary>登陆界面 确认按钮 事件</summary>
    /// <param name="form">按钮所在窗体</param>
    /// <param name="user">登陆用户名</param>
    /// <param name="pwd">登陆密码</param>
    public delegate void LoginEventHandler(Form form, string user, string pwd);
    /// <summary>登陆界面 取消按钮 事件</summary>
    /// <param name="form">按钮所在窗体</param>
    public delegate void LoginCancelEventHandler(Form form);
}
